package com.gnkexy.springframework.test;

import com.gnkexy.springframework.beans.BeansException;
import com.gnkexy.springframework.beans.PropertyValue;
import com.gnkexy.springframework.beans.PropertyValues;
import com.gnkexy.springframework.beans.factory.config.BeanDefinition;
import com.gnkexy.springframework.beans.factory.config.BeanReference;
import com.gnkexy.springframework.beans.factory.support.DefaultListableBeanFactory;
import com.gnkexy.springframework.test.bean.IUserDao;
import com.gnkexy.springframework.test.bean.UserDao;
import com.gnkexy.springframework.test.bean.UserService;
import org.junit.Test;

public class Apitest2 {


    @Test
    public void test() throws BeansException {
        //注册bean工厂
        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();

        //注入userdao
        BeanDefinition beanDefinition = new BeanDefinition(UserDao.class);
        beanFactory.registerBeanDefinition("userDao",beanDefinition);

        //定义属性
        PropertyValues propertyValues = new PropertyValues();
        propertyValues.addProperty(new PropertyValue("uid","0002"));
        propertyValues.addProperty(new PropertyValue("userDao",new BeanReference("userDao")));

        //填充属性
        BeanDefinition beanDefinition1 = new BeanDefinition(UserService.class, propertyValues);
        beanFactory.registerBeanDefinition("userService",beanDefinition1);

        UserService userService = (UserService) beanFactory.getBean("userService");
        String s = userService.queryUserInfo();
        System.out.println(s);

    }

    @Test
    public void test2(){
        //注册bean工厂
        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();

        //定义属性
        PropertyValues propertyValues = new PropertyValues();
        propertyValues.addProperty(new PropertyValue("name","dww"));

        //填充属性
//        BeanDefinition beanDefinition1 = new BeanDefinition(InfoService.class, propertyValues);
//        beanFactory.registerBeanDefinition("infoService",beanDefinition1);
//
//        InfoService infoService = (InfoService) beanFactory.getBean("infoService");
//        infoService.queryUserInfo();

    }

}
